package com.lp.mgt.services.impls;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lp.mgt.entities.Role;
import com.lp.mgt.entities.User;
import com.lp.mgt.mappers.RoleMapper;
import com.lp.mgt.services.RoleService;
import com.lp.mgt.utils.UUIDUtil;
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Override
	public Map<String, Object> getUsersWithTerms(Role role) {
		Map<String,Object> map=new HashMap<>();
		PageHelper.startPage(role.getPage(), role.getLimit());
		List<Role> list=roleMapper.getUserDataWithTerms(role);
		PageInfo<Role> pageInfo = new PageInfo<>(list);
		map.put("count",pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		return map;
	}

	@Override
	public Boolean updateRoleByIdWhenEdit(Role role) {
		int count=roleMapper.updateRoleByIdWhenEdit(role);
		return count==1?true:false;
	}
	
	@Override
	public Boolean deleteRoleById(String rid) {
		roleMapper.deleteRoleAndUsers(rid);
		roleMapper.deleteRoleAndMenus(rid);
		roleMapper.deleteRoleById(rid);
		//按理说还要删除关联表数据如mgt_role_menu,mgt_user_role等
		return true;
	}

	@Override
	public Boolean saveRolesByBatch(List<Role> roles) {
		for(Role r:roles) {
			r.setRid(UUIDUtil.getUUID());
			r.setDelFlag("0");
			r.setCreateTime(new Date());
		}
		int count=roleMapper.saveRolesByBatch(roles);
		//按理说还要删除关联表数据如mgt_role_menu,mgt_user_role等
		return count==roles.size()?true:false;
	}
}
